package cn.com.chengmandian.bike.controller;

import cn.com.chengmandian.bike.model.EbDeviceBike;
import cn.com.chengmandian.bike.model.EbMemberInfo;
import cn.com.chengmandian.bike.model.EbRideClose;
import cn.com.chengmandian.bike.model.EbRideOrder;
import cn.com.chengmandian.bike.service.*;
import cn.com.chengmandian.core.common.constants.CommonConstant;
import cn.com.chengmandian.core.redis.cache.AgentParamCacheService;
import cn.com.chengmandian.core.web.pojo.AjaxResult;
import cn.com.chengmandian.service.bike.constants.*;
import cn.com.chengmandian.service.bike.form.*;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;

@Slf4j
@Api(tags = "骑行相关接口 - 微服务")
@RestController
@RequestMapping("/inner/ride")
public class InnerRideController {
    @Autowired
    private IEbRideCommandService rideCommandService;

    @Autowired
    private IEbRideOrderService rideOrderService;

    @Autowired
    private IEbDeviceBikeService deviceBikeService;

    @Autowired
    private IEbMemberCardService memberCardService;

    @Autowired
    private IEbRentOrderService rentOrderService;

    @Autowired
    private IEbMemberComboRecordService memberComboRecordService;

    @Autowired
    private IEbMemberInfoService memberInfoService;

    @Autowired
    private AgentParamCacheService agentParamCacheService;

    @Autowired
    private IEbRidePenaltyService ridePenaltyService;

    @Autowired
    private IEbRideCloseService rideCloseService;

    @ApiOperation("运维结束订单")
    @PostMapping("/order/close/ops")
    public AjaxResult opsCloseOrder(@RequestBody OpsCloseOrderForm opsCloseOrderForm) {
        Assert.notNull(opsCloseOrderForm.getOrderId(), "缺少订单ID");
        EbRideClose rideClose = new EbRideClose();
        rideClose.setPortParams(JSONObject.toJSONString(opsCloseOrderForm));
        Date portStartTime = new Date();
        rideClose.setPortStartTime(portStartTime);
        rideCloseService.opsCloseOrder(rideClose, opsCloseOrderForm.getOrderId(), opsCloseOrderForm.getCloseReason(), opsCloseOrderForm.getOpsUserId(), opsCloseOrderForm.getOpsUserName(), opsCloseOrderForm.getOrderMoney());
        rideOrderService.opsCloseLock(opsCloseOrderForm.getOrderId(), opsCloseOrderForm.getCloseReason(), opsCloseOrderForm.getOpsUserId(), opsCloseOrderForm.getOpsUserName(), opsCloseOrderForm.getOrderMoney());
        return AjaxResult.success();
    }

    @ApiOperation("代理端结束订单")
    @PostMapping("/order/close/agent")
    public AjaxResult agentCloseOrder(@RequestBody AgentCloseOrderForm agentCloseOrderForm) {
        Assert.notNull(agentCloseOrderForm.getOrderId(), "缺少订单ID");
        EbRideClose rideClose = new EbRideClose();
        rideClose.setPortParams(JSONObject.toJSONString(agentCloseOrderForm));
        Date portStartTime = new Date();
        rideClose.setPortStartTime(portStartTime);
        rideCloseService.agentCloseOrder(rideClose, agentCloseOrderForm.getOrderId(), agentCloseOrderForm.getUserId(), agentCloseOrderForm.getUserName(), agentCloseOrderForm.getCustomMoney());
        rideOrderService.agentCloseLock(agentCloseOrderForm.getOrderId(), agentCloseOrderForm.getCustomMoney(), agentCloseOrderForm.getUserId(), agentCloseOrderForm.getUserName());
        return AjaxResult.success();
    }

    @ApiOperation("系统结束订单")
    @PostMapping("/order/close/sys")
    public AjaxResult sysCloseOrder(@RequestBody SysCloseOrderForm sysCloseOrderForm) {
        Assert.notNull(sysCloseOrderForm.getOrderId(), "缺少订单ID");
        EbRideClose rideClose = new EbRideClose();
        rideClose.setPortParams(JSONObject.toJSONString(sysCloseOrderForm));
        Date portStartTime = new Date();
        rideClose.setPortStartTime(portStartTime);
        rideCloseService.sysCloseOrder(rideClose, sysCloseOrderForm.getOrderId(), sysCloseOrderForm.getSysName());
        rideOrderService.sysCloseLock(sysCloseOrderForm);
        return AjaxResult.success();
    }

    @ApiOperation("系统临停订单")
    @PostMapping("/order/stop/sys")
    public AjaxResult sysStopOrder(@RequestBody SysStopOrderForm sysStopOrderForm) {
        Assert.notNull(sysStopOrderForm.getOrderId(), "缺少订单ID");
        rideOrderService.sysStopLock(sysStopOrderForm);
        return AjaxResult.success();
    }

    @ApiOperation("车辆一键还车")
    @PostMapping("/bike/close/order")
    public AjaxResult bikeCloseOrder(@RequestBody BikeCloseOrderForm form){
        Assert.notNull(form.getBikeId(), "缺少车辆ID");
        EbRideOrder rideOrder = rideOrderService.getByBike(form.getBikeId());
        if(rideOrder == null) return AjaxResult.success();
//        EbMemberInfo memberInfo = memberInfoService.getById(rideOrder.getMemberId());

//        EbDeviceBike deviceBike = deviceBikeService.getById(form.getBikeId());
//         检验车辆是否可结束骑行
//        deviceBikeService.checkCloseBike(deviceBike, rideOrder, CommonConstant.ZERO, CommonConstant.ZERO, memberInfo.getId(), memberInfo.getName());
//        rideOrderService.memberCheckStop(rideOrder.getOrderNo(), memberInfo.getId(), CommonConstant.ZERO);
        EbRideClose rideClose = new EbRideClose();
        rideClose.setPortParams(JSONObject.toJSONString(form));
        Date portStartTime = new Date();
        rideClose.setPortStartTime(portStartTime);
        rideCloseService.bikeCloseOrder(rideClose, form.getBikeId());
        rideOrderService.bikeCloseOrder(form.getBikeId());
        return AjaxResult.success();
    }

    @ApiOperation("订单退款")
    @PostMapping("/refund")
    public AjaxResult rideRefund(@RequestBody RideRefundForm form){
        Assert.notNull(form.getRideOrderId(),"缺少订单id");
        if(form.getRefundType().equals(RefundTypeEnum.DISPATCH.getCode()) || form.getRefundType().equals(RefundTypeEnum.HELMET.getCode())){
            EbRideOrder rideOrder = rideOrderService.getById(form.getRideOrderId());
            if (rideOrder.getPenaltyPayStatus().equals(RideOrderPenaltyPayStatusEnum.COMPLETE.getCode())) {
                ridePenaltyService.refundByOrder(new RidePenaltyRefundForm()
                        .setRideOrderId(form.getRideOrderId())
                        .setRefundType(form.getRefundType())
                        .setRefundMoney(form.getRefundMoney())
                        .setRefundReason(form.getRefundReason())
                        .setLoginUser(form.getLoginUser()));
                return AjaxResult.success();
            }
        }
        rideOrderService.rideRefund(form);
        return AjaxResult.success();
    }

    @ApiOperation("骑行卡退款")
    @PostMapping("/card/refund")
    public AjaxResult rideCardRefund(@RequestBody RideCardRefundForm form){
        Assert.notNull(form.getMemberCardId(),"缺少用户骑行卡id");
        Assert.notNull(form.getRefundMoney(),"缺少退款金额");
        memberCardService.refund(form);
        return AjaxResult.success();
    }

    @ApiOperation("会员卡退款")
    @PostMapping("/combo/refund")
    public AjaxResult memberComboRefund(@RequestBody MemberComboRefundForm form){
        Assert.notNull(form.getPayId(), "缺少支付信息");
        Assert.notNull(form.getLoginUser(), "缺少登录用户");
        Assert.isTrue(form.getMoney() != null && form.getMoney().compareTo(BigDecimal.ZERO) > 0, "缺少金额");
        memberComboRecordService.refund(form);
        return AjaxResult.success();
    }

    @ApiOperation("订单金额修改（骑行中）")
    @PostMapping("/update/money")
    public AjaxResult orderUpdateMoney(@RequestBody RideSysUpdateMoneyForm form){
        Assert.notNull(form.getOrderId(), "缺少订单id");
        Assert.notNull(form.getAgentUser(), "缺少操作人");
        EbRideOrder rideOrder = rideOrderService.getById(form.getOrderId());
        Assert.notNull(rideOrder, "订单不存在");
        EbRideClose rideClose = new EbRideClose();
        rideClose.setPortParams(JSONObject.toJSONString(form));
        Date portStartTime = new Date();
        rideClose.setPortStartTime(portStartTime);
        BigDecimal customOrderMoney = BigDecimal.ZERO;
        if(form.getHelmetMoney() != null) {
            customOrderMoney = customOrderMoney.add(form.getHelmetMoney());
        }
        if(form.getDispatchMoney() != null) {
            customOrderMoney = customOrderMoney.add(form.getDispatchMoney());
        }
        if(form.getOrderMoney() != null) {
            customOrderMoney = customOrderMoney.add(form.getOrderMoney());
        }
        rideCloseService.agentCloseOrder(rideClose, form.getOrderId(), form.getAgentUser().getId(), form.getAgentUser().getName(), customOrderMoney);
        rideOrderService.orderUpdateMoney(form.getAgentUser(), rideOrder, form);
        return AjaxResult.success();
    }

    @ApiOperation("罚款金额退款")
    @PostMapping("/penalty/refund")
    public AjaxResult penaltyRefund(@RequestBody RidePenaltyRefundForm form) {
        Assert.notNull(form.getLoginUser(), "缺少操作人");
        ridePenaltyService.refundByOrder(form);
        return AjaxResult.success();
    }
}
